package com.his.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.his.admin.service.HospitalService;
import com.his.dao.domain.dto.ListDto;
import com.his.dao.domain.entity.Hospital;
import com.his.dao.domain.entity.MInterface;
import com.his.dao.domain.vo.ListVo;
import com.his.dao.mapper.HospitalMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.his.util.result.RestResult;
import com.his.util.result.RestResultBuilder;
import com.his.util.util.MapUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fall
 * @since 2022-11-28
 */
@Service
@AllArgsConstructor
public class HospitalServiceImpl extends ServiceImpl<HospitalMapper, Hospital> implements HospitalService {
    private HospitalMapper hospitalMapper;

    @Override
    public RestResult all(ListDto listDto) {
        Hospital hospital = MapUtil.conversionMapToObj((LinkedHashMap) listDto.getObj(), Hospital.class);;
        IPage iPage = new Page(listDto.getPage(), listDto.getPageSize());
        QueryWrapper<Hospital> queryWrapper = new QueryWrapper<Hospital>()
                .like(hospital.getName() != null, "name", hospital.getName())
                .like(hospital.getHospitalType() != null, "hospital_type", hospital.getHospitalType())
                .like(hospital.getAddress() != null, "address", hospital.getAddress())
                .eq(hospital.getStatus() != null, "status", hospital.getStatus());
        IPage hospitalPage = hospitalMapper.selectPage(iPage, queryWrapper);

        return new RestResultBuilder().success(ListVo
                .builder()
                .list(hospitalPage.getRecords())
                .total(hospitalPage.getTotal())
                .build());
    }

    @Override
    public RestResult allId(Hospital hospital) {
        QueryWrapper<Hospital> queryWrapper = new QueryWrapper<Hospital>()
                .select("hospital_id", "name")
                .like(hospital.getName() != null, "name", hospital.getName());
        return new RestResultBuilder().success(hospitalMapper.selectList(queryWrapper));
    }
}
